package main

import (
    "fmt"
    "strings"
)

func solution(s string, k int) string {
    // write code here
    if k <= 0 {
        return s
    }

    var res strings.Builder
    for _, c := range s {
        if c == 'a' {
            res.WriteString("bc")
        }
        if c == 'b' {
            res.WriteString("ca")
        }
        if c == 'c' {
            res.WriteString("ab")
        }
    }
    k -= 1

    return solution(res.String(), k)
}

func solution1(s string, k int) string {
    if k <= 0 {
        return s
    }

    // 使用map存储映射规则
    mapping := map[rune]string{
        'a': "bc",
        'b': "ca",
        'c': "ab",
    }

    // 使用strings.Builder收集字符提高效率
    var res strings.Builder
    for _, c := range s {
        res.WriteString(mapping[c])
    }

    // 递归调用
    return solution1(res.String(), k-1)
}

func main() {
    fmt.Println(solution("abc", 2) == "caababbcbcca")
    fmt.Println(solution("abca", 3) == "abbcbccabccacaabcaababbcabbcbcca")
    fmt.Println(solution("cba", 1) == "abcabc")

    fmt.Println(solution1("abc", 2) == "caababbcbcca")
    fmt.Println(solution1("abca", 3) == "abbcbccabccacaabcaababbcabbcbcca")
    fmt.Println(solution1("cba", 1) == "abcabc")
}